export default {
  namespaced: true,
  state: {
    max: 5, // 最多可以对比的商品数目
    // 需要对比的商品列表
    prods: [],
  },
  getters: {
    getProds(state) {
      return state.prods.length;
    },
    getProdsLength: (state) => state.prods.length,
  },
  actions: {},
  mutations: {
    addComparison(state, product) {
      if (state.prods.length < state.max) {
        // 先判断是否已经在对比列表中
        const index = state.prods.findIndex((item) => item.id === product.id);
        if (index === -1) {
          state.prods.push(product);
        }
      }
    },
    deleteComparison(state, product) {
      const index = state.prods.findIndex((item) => item.id === product.id);
      if (index !== -1) {
        state.prods.splice(index, 1);
      }
    },
    emptyComparison(state) {
      state.prods = [];
    },
  },
};
